package com.chijun.mform.process.view;


import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.chijun.mform.FormUtils;
import com.chijun.mform.R;
import com.chijun.mform.full.mode.ModeFormFullApprovePerson;
import com.chijun.mform.process.ModeServerFullData;
import com.example.ccj.b.c.custom.image.IImageChangeDelete;
import com.example.ccj.b.c.custom.image.IImageChangeUpload;
import com.example.ccj.b.c.custom.image.ImageChangeListener;
import com.example.ccj.b.c.custom.image.LoadImagesRecycler;
import com.example.ccj.b.c.custom.image.MLocalMedia;
import com.example.ccj.b.c.dialog.LoadingDialog;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.ToastUtils;
import com.example.ccj.u.tengxun.cos.CosDeleteCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取审批内容的模块
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/12/29 2:12
 */
public class ViewGetShenPiContentView extends LinearLayout {

    ModeFormFullApprovePerson person;
    ModeServerFullData serverFullData;
    public StringBuilder imgStr;
    public String opinionStr;

    String FILE_PREFIX;
    public ViewGetShenPiContentView(Context context, ModeServerFullData serverFullData, ModeFormFullApprovePerson person) {
        super(context);
        this.serverFullData = serverFullData;
        this.person = person;
        FILE_PREFIX = "/"+serverFullData.launchedForm.formId + "/" + person.formFullId+"/" + person.processId +"/"+person.id+"/";
        imgStr = new StringBuilder();
        init();
    }

    public ViewGetShenPiContentView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public void sateData(ModeServerFullData serverFullData, ModeFormFullApprovePerson person){
        this.serverFullData = serverFullData;
        this.person = person;
        FILE_PREFIX = "/"+serverFullData.launchedForm.formId + "/" + person.formFullId+"/" + person.processId +"/"+person.id+"/";
        imgStr = new StringBuilder();
        init();
    }

    private void init(){
        LinearLayout.inflate(getContext(), R.layout.view_add_ping_lun,this);
        EditText opinion = findViewById(R.id.view_add_ping_lun_opinion);
        LoadImagesRecycler imagesRecycler = findViewById(R.id.view_add_ping_lun_img_rec);

        if (StringUtils.isNoEmpty(person.opinion)) opinion.setText(person.opinion);

        opinion.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                opinionStr = s.toString();
            }
        });

        List<MLocalMedia> localMediaList = new ArrayList<>();
        imagesRecycler.setData(localMediaList);
        imagesRecycler.setImageChangeListener(new ImageChangeListener() {
            @Override
            public void imageNetWorkDelete(MLocalMedia images, IImageChangeDelete iImageChangeDelete) {
                LoadingDialog loadingDialog = new LoadingDialog(getContext());
                loadingDialog.showLoading();

                FormUtils.cosDeleteFile(getContext(), images.urlPath, new CosDeleteCallBack() {
                    @Override
                    public void onComplete(String s) {
                        iImageChangeDelete.onDelete(images);
                        loadingDialog.dismiss();
                        int i = imgStr.indexOf(s);
                        imgStr.delete(imgStr.indexOf(s), i + s.length());
                    }

                    @Override
                    public void onError(String s) {
                        ToastUtils.showShort(s);
                        loadingDialog.dismiss();
                    }
                });
            }

            @Override
            public void imageInsertNetWork(List<MLocalMedia> nowImages, IImageChangeUpload iImageChangeUpload) {
                LoadingDialog loadingDialog = new LoadingDialog(getContext());
                loadingDialog.showUpload(100);

                for (MLocalMedia nowImage : nowImages) {
                    nowImage.urlPath = FILE_PREFIX + nowImage.getFileName();
                }

                FormUtils.cosUploadListMedia(getContext(), nowImages, new FormUtils.UploadMediaListener() {
                    @Override
                    public void onSleep(double v) {
                        loadingDialog.setSpeed((int) v);
                    }

                    @Override
                    public void onComplete(MLocalMedia localMedia) {
                        LogUtils.d("图片上传完毕一张！");
                    }

                    @Override
                    public void onCompleteAll(List<MLocalMedia> localMediaList) {
                        LogUtils.d("图片全部上传完毕！");
                        for (MLocalMedia mLocalMedia : localMediaList) {
                            imgStr.append(mLocalMedia.urlPath).append(",");
                        }
                        iImageChangeUpload.onUpload(nowImages);
                    }

                    @Override
                    public void onError(String s) {
                        loadingDialog.dismiss();
                    }
                });
            }
        });



    }






}
